package com.example.dell.startproject_122zzy.model;

import com.example.dell.startproject_122zzy.bean.WeatherResult;
import com.example.dell.startproject_122zzy.iface.WeatherIface;
import com.example.dell.startproject_122zzy.listener.Weatherlistener;
import com.example.dell.startproject_122zzy.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dell on 2017/3/14.
 */

public class WeatherModel implements WeatherIface{
    private WeatherService weatherService;
private Retrofit retrofit;
    private static final String BASE_URL=
            "http://www.weather.com.cn/";


    public WeatherModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
        .build();
    }
    @Override
    public void getWeather(String cityno, final Weatherlistener Listener) {
        //Retrofit使用3-2
        weatherService=retrofit.create(WeatherService.class);
        Call<WeatherResult> call=weatherService.getResult(cityno);
        //Retrofit使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful()&& response.body()!=null)
                    Listener.onResponse(response.body().getWeatherinfo());
                else
                    Listener.onFail("解析错误!");

            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
               Listener.onFail(t.toString());
            }
        });
    }
}
